package net.maku.qdd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.qdd.common.utils.PageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.maku.qdd.convert.MerchantConvert;
import net.maku.qdd.entity.MerchantEntity;
import net.maku.qdd.query.MerchantQuery;
import net.maku.qdd.vo.MerchantVO;
import net.maku.qdd.dao.MerchantDao;
import net.maku.qdd.service.MerchantService;
import net.maku.qdd.common.utils.ExcelUtils;
import net.maku.qdd.vo.MerchantExcelVO;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商家
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantDao, MerchantEntity> implements MerchantService {

    @Override
    public PageResult<MerchantVO> page(MerchantQuery query) {
        IPage<MerchantEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(MerchantConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    /**
     * 获取分页对象
     */
    private IPage<MerchantEntity> getPage(MerchantQuery query) {
        return new Page<>(query.getCurrent(), query.getSize());
    }

    /**
     * 获取查询包装器
     */
    private LambdaQueryWrapper<MerchantEntity> getWrapper(MerchantQuery query){
        LambdaQueryWrapper<MerchantEntity> wrapper = Wrappers.lambdaQuery();
        // 这里可以添加具体的查询条件
        return wrapper;
    }


    @Override
    public MerchantVO get(Long id) {
        MerchantEntity entity = baseMapper.selectById(id);
        MerchantVO vo = MerchantConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MerchantVO vo) {
        MerchantEntity entity = MerchantConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MerchantVO vo) {
        MerchantEntity entity = MerchantConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
        List<MerchantExcelVO> excelList = MerchantConvert.INSTANCE.convertExcelList(list());
        ExcelUtils.excelExport(MerchantExcelVO.class, "商家", null, excelList);
    }

}